<template>
  <el-card style="max-width:480px;margin:200px auto">
    <h1>欢迎登录用户管理系统</h1>
    <el-from :model="form" label-width="auto" style="max-width: 480px">
      <el-form-item label="账号">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login" class="login">登录</el-button>
      </el-form-item>
    </el-from>
  </el-card>
</template>

<script>
import {ElMessage} from "element-plus";
export default {
  name:"LoginView",
  data(){
    return{
      form:{username:'', password:''}
    }
  },
    methods:{
      login(){
        //调用登录的方法
        this.$axios.post('/login',this.$qs.stringify(this.form))
            .then(result=>{
              console.log('login',result.data)
              //判断是否连接成功
              if (result.data.code===200){
                //判断用户是否存在
                if(result.data.data){
                  //跳转到主界面
                  ElMessage.success('登陆成功')

                  //保存用户信息
                  localStorage.setItem('user',JSON.stringify(result.data.data))

                    //跳转方式2：JS代码跳转
                    this.$router.push({path:'/index'})
                }else{
                  ElMessage.error('账号或密码错误')
                }
              }
            })
      }
    }
  }

</script>
<style scoped>
.login{
  margin: 20px auto;
  height: 40px;
  width: 250px;
  display: flex;
  background-color: #5cb85c;
}
</style>
